<?php

namespace App\Models;

use App\Facades\Site as CollAreaSite;

use Illuminate\Database\Eloquent\Model;
use Cache, Validator;
use Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest;

class CollArea extends Model
{
    protected $table = 'coll_area';
    //设置主键值
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = [
        'cid', 'name', 'grade', 'pid', 'sort'
    ];

    /**
     * $parm  $id 当前选项ID
     * 获取当前选项地区自己ID
     * @return array
     */
    public static function CurrArea($id)
    {
        $currArr = self::find($id)->toArray();
        $arr = array();
        if ($currArr) {
            if ($currArr['grade'] == 1) {
                $arr[] = $currArr['id'];
                //当前省地区
                $area = self::where('grade', 2)->where('pid', $currArr['cid'])->orderBy('cid', 'asc')->get();
                $area = setSort($area);
                foreach ($area as $k => $v) {
                    $arr[] = $v['id'];
                    //当前省地区县级市
                    $city = self::where('grade', 3)->where('pid', $v->cid)->orderBy('cid', 'asc')->get();
                    $city = setSort($city);
                    foreach ($city as $vv) {
                        $arr[] = $vv['id'];
                    }
                }
                array_push($arr, $currArr['id']);
            } else if ($currArr['grade'] == 2) {
                $arr = array_column(self::where('pid', $currArr['cid'])->get()->toArray(), 'id');
                array_push($arr, $currArr['id']);
            } else {
                $arr[] = $currArr['id'];
            };
            return $arr;
        } else {
            return array();
        }
    }

    //--------------------------------------------------------------------------------------------------------------------------------

    /**
     * 提取省份
     * @return array
     */
    public static function getProvince()
    {
        $area = self::where('grade', 1)->orderBy('id', 'asc')->get();
        //dd($area);
//        $area = setSort($area);
        $areaArray = array();
        foreach ($area as $key => $value) {
            $areaArray[] = array('id' => $value['cid'], 'name' => $value['name']);
        }
        return $areaArray;
    }

    /**
     * 提取地级市
     * @return array
     * $pid:省份的id
     */
    public static function getArea($pid)
    {
        $area = self::where('grade', 2)->where('pid', $pid)->orderBy('id', 'asc')->get();
//        $area = setSort($area);
        $areaArray = array();
        foreach ($area as $key => $value) {
            $areaArray[] = array('id' => $value['cid'], 'name' => $value['name']);
        }
        return $areaArray;
    }

    /**
     * 提取县市区
     * @return array
     * $pid:地级市id
     */
    public static function getCity($pid)
    {
        $area = self::where('grade', 3)->where('pid', $pid)->orderBy('id', 'asc')->get();
//        $area = setSort($area);
        $areaArray = array();
        foreach ($area as $key => $value) {
            $areaArray[] = array('id' => $value['cid'], 'name' => $value['name']);
        }
        return $areaArray;
    }

    /**
     * 提取省和地区//二维
     * @return array
     */
    public static function getProvinceArea()
    {
        $provinceArray = array();
        if (Cache::has('areaProvinceArea')) {
            $provinceArray = Cache::get('areaProvinceArea');
        } else {
            $province = self::where('grade', 1)->get();
            $province = setSort($province);
            foreach ($province as $key => $value) {
                $area = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $area = setSort($area);
                $areaArray = array();
                foreach ($area as $k => $v) {
                    $areaArray[] = array('value' => $v['name'], 'text' => $v['cid']);
                }
                $provinceArray[] = array('value' => $value['name'], 'text' => $value['cid'], 'area' => $areaArray);
            }
            Cache::forever('areaProvinceArea', $provinceArray);
        }
        return $provinceArray;
    }

    /**
     * 提取省和地区和县市区//三维
     * @return array
     */
    public static function getComplete()
    {
        $provinceArray = array();
        if (Cache::has('areaComplete')) {
            $provinceArray = Cache::get('areaComplete');
        } else {
            $province = self::where('grade', 1)->get();
            $province = setSort($province);
            foreach ($province as $key => $value) {
                $area = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $area = setSort($area);
                $areaArray = array();
                foreach ($area as $k => $v) {
                    $city = self::where('grade', 3)->where('pid', $v['cid'])->get();
                    $city = setSort($city);
                    $cityArray = array();
                    foreach ($city as $city_k => $city_v) {
                        $cityArray[] = array('value' => $v['name'], 'text' => $v['cid']);
                    }
                    $areaArray[] = array('value' => $v['name'], 'text' => $v['cid'], 'city' => $cityArray);
                }
                $provinceArray[] = array('value' => $value['name'], 'text' => $value['cid'], 'area' => $areaArray);
            }
            Cache::forever('areaComplete', $provinceArray);
        }
        return $provinceArray;
    }

    /**
     * 获取地区的省市县三级中文
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getAreaAddress($cid)
    {
        if ($cid == '') return '';
        $province = substr($cid, 0, 2) . '0000';
        $areaId = substr($cid, 0, 4) . '00';
        $province = self::getAreaName($province);
        $area = self::getAreaName($areaId);

        if ($areaId == $cid) {
            return $province . $area;
        }
        $city = self::getAreaName($cid);
        return $province . $area . $city;
    }

    public static function getAreaAddressThree($cid)
    {
        if ($cid == '') return '';
        $province = substr($cid, 0, 2) . '0000';
        $areaId = substr($cid, 0, 4) . '00';
        $province = self::getAreaName($province);
        $area = self::getAreaName($areaId);

        if ($areaId == $cid) {
            return [$province, $area, ''];
        }
        $city = self::getAreaName($cid);
        return [$province, $area, $city];
    }

    /**
     * 获取地区的市县二级中文
     * $cid:选项编号
     * @return： 当前城市地区
     */
    public static function getCityAddress($cid)
    {
        if ($cid == '') return '';
        //$province = substr($cid,0,2) . '0000';
        $area = substr($cid, 0, 4) . '00';
        // $province= self::where('cid',$province)->first();
        $area = self::getAreaName($area);
        $city = self::getAreaName($cid);
        return $area . ',' . $city;
    }

    /**
     * 获取地区的中文
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getAreaName($cid)
    {
        $city = self::where('cid', $cid)->first();
        if (isset($city)) {
            return $city->name;
        } else {
            $town = CollTown::getTownName($cid);
            if ($town) {
                return $town;
            } else {
                return '';
            }
        }
    }

    /**
     * 获取当前站点的城市
     */
    public static function getSiteCity()
    {
        $webareacode = substr(CollAreaSite::get('webareacode'), 0, 4) . '00';
        return self::getCity($webareacode);
    }


    /**
     * 清理本模型中的所有缓存数据
     */
    public static function clearCcache()
    {
        Cache::forget('areaComplete');
        Cache::forget('areaProvinceArea');
        return '';
    }

    /**
     * 说明:获取以为全部 省份市区县级市
     */
    public static function getAreaAll()
    {
        if (Cache::has('area_all')) {
            $area_all = Cache::get('area_all');
        } else {
            $area_all = self::select('cid', 'name')->get();
            if ($area_all) Cache::forever('area_all', $area_all);
        }
        return $area_all;
    }
}